package com.wonders.frame.chron.controller.catalog;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wonders.frame.chron.controller.BasicChronController;
import com.wonders.frame.chron.model.bo.Catalog;
import com.wonders.frame.chron.service.catalog.CatalogService;
import com.wonders.frame.chron.util.ConstantUtil;
import com.wonders.frame.chron.util.StringUtil;
import com.wonders.frame.core.dao.RelationDao;
import com.wonders.frame.core.model.IDefaultModel;
import com.wonders.frame.core.model.bo.Relation;
import com.wonders.frame.core.model.vo.RelatedNode;
import com.wonders.frame.core.model.vo.ReturnObj;
import com.wonders.frame.core.model.vo.ReturnObjInfo;

@Controller
@RequestMapping("/catalog")
public class CatalogController extends BasicChronController<Catalog> {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private CatalogService catalogService;
	@Autowired
	private RelationDao relationDao;
	@RequestMapping(value = "/getCode/{id}", method = RequestMethod.GET)
	@ResponseBody
	public ReturnObj getCode(
			@PathVariable("id") Integer id,
			@RequestParam(value = "sortField", required = false, defaultValue = "") String sortField,
			@RequestParam(value = "sortAsc", required = false, defaultValue = "asc") String sortAsc,
			@RequestParam(value = "objFilter", required = false, defaultValue = "") String neededObj,
			@RequestParam(value = "lv", required = false, defaultValue = "-1") int lv,
			@RequestParam(value = "ticket", required = true) String ticket) {
		// 得到当前节点信息
		
		ReturnObj obj = get(id);
		if (obj.getInfo().getSuccess()) {
			Catalog c = (Catalog) obj.getData();
			String property = c.getProperty();
			if (property.equals("first")) {
				obj.setData(c.getCode());
				return obj;
			} else if (property.equals("secend")) {
				lv = 1;
			} else if (property.equals("third")) {
				lv = 2;
			} else if (property.equals("fourth")) {
				lv = 3;
			}

			
			if (StringUtil.isNull(neededObj)) {
				neededObj += "&objFilter=nType,catalog";
			}
			ReturnObj<List<RelatedNode<IDefaultModel, Integer>>> parentNode = getParentNode(
					id, sortField, sortAsc, neededObj, lv, ticket);

			String codes = "";
			List<RelatedNode<IDefaultModel, Integer>> data = parentNode
					.getData();
			String first = "";
			String secend = "";
			String third = "";
			String fourth = "";
			if (parentNode.getInfo().getSuccess()) {
				for (RelatedNode<IDefaultModel, Integer> relatedNode : data) {
					if ("catalog".equals(relatedNode.getNobj())) {
						Catalog c2 = (Catalog) relatedNode.getNode();
						if (c2.getProperty().equals("first")) {
							first =  c2.getCode();
						} else if (c2.getProperty().equals("secend")) {
							secend = "-" + c2.getCode();
						} else if (c2.getProperty().equals("third")) {
							third = "-" + c2.getCode();
						} else if (c2.getProperty().equals("fourth")) {
							fourth = "-" + c2.getCode();
						}
					}
				}
			}
			codes = first + secend + third + fourth + "-" + c.getCode();
			obj.setData(codes);
		}
		return obj;
	}

	//添加长编目录
	@RequestMapping(value = "/copyCatalogs", method = RequestMethod.POST)
	@ResponseBody
	public String copyCatalogs(@RequestParam("ids") String ids,
			@RequestParam(value = "sortField", required = false, defaultValue = "") String sortField,
			@RequestParam(value = "sortAsc", required = false, defaultValue = "asc") String sortAsc,
			@RequestParam(value = "objFilter", required = false, defaultValue = "") String neededObj,
			@RequestParam(value = "lv", required = false, defaultValue = "-1") int lv,
			@RequestParam(value = "ticket", required = true) String ticket,
			HttpServletRequest request){
		
		if(ids==null)
			return null;
		
		String type="summary";
		String pType=this.getEntityName();
		for (String cid : StringUtils.split(ids, ",")) {
			//得到当前节点的子级节点
			Catalog c = get(cid);
		
			//添加长编根节点
			Catalog newC=new Catalog(c.getName(),c.getProperty(), type, 0, c.getCode(), c.getResourceType(),c.getParentCode());
			//添加目录
			catalogService.save(newC);
			//添加根节点关系
			Relation relation=new Relation(pType,ConstantUtil.CATALOG_ROOT_ID , pType, newC.getId(), 1);
			relationDao.save(relation);

			this.getChildNode(c.getId()+"",newC.getId(),type, sortField, sortAsc, neededObj, lv, ticket);
		}
		
		return null; 
	}
	
	
	private void getChildNode(String cid,Integer pid,String type,String sortField, String sortAsc, String neededObj, int lv, String ticket){
		
		ReturnObj<List<RelatedNode<IDefaultModel,Integer>>> childNode = getChildNode(Integer.valueOf(cid), sortField, sortAsc, neededObj, lv, ticket);
		//添加关系
		if(childNode.getInfo().getSuccess()){
			for (RelatedNode<IDefaultModel, Integer> node : childNode.getData()) {
				Catalog c1=(Catalog) node.getNode();
				Catalog newC=new Catalog(c1.getName(),c1.getProperty(), type, 0, c1.getCode(), c1.getResourceType(),c1.getParentCode());
				catalogService.save(newC);
				//添加根节点关系
				Relation relation=new Relation("catalog", pid, "catalog", newC.getId(),1);
				relationDao.save(relation);
				this.getChildNode(c1.getId()+"",newC.getId(), type, sortField, sortAsc, neededObj, lv, ticket);
			}
		}
	}
	
	@RequestMapping(value = "/copy")
	@ResponseBody
	public ReturnObj copy(@RequestParam("cid") String id,
			@RequestParam(value = "sortField", required = false, defaultValue = "") String sortField,
			@RequestParam(value = "sortAsc", required = false, defaultValue = "asc") String sortAsc,
			@RequestParam(value = "objFilter", required = false, defaultValue = "") String neededObj,
			@RequestParam(value = "lv", required = false, defaultValue = "-1") int lv,
			@RequestParam(value = "ticket", required = true) String ticket,
			@RequestParam(value = "code", required = false) String code,
			HttpServletRequest request){

		ReturnObj obj=new ReturnObj<Catalog>(false);
		try {
			Catalog c = get(id);
			Catalog newC=new Catalog(c.getName(), c.getProperty(), c.getType(), 0,code, c.getResourceType(),c.getParentCode());
			catalogService.save(newC);
			obj.setInfo(new ReturnObjInfo(true));
			obj.setData(newC);
			getChildNode(id, newC.getId(), newC.getType(), sortField, sortAsc, neededObj, lv, ticket);
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		
		return obj;
	}

		
}
	